package cn.itsource.org.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.digest.MD5;
import cn.hutool.http.server.HttpServerRequest;
import cn.itsource.basic.config.exception.BusinessException;
import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.basic.util.*;
import cn.itsource.order.domain.OrderAddress;
import cn.itsource.order.domain.OrderAdopt;
import cn.itsource.order.domain.ParamDo;
import cn.itsource.order.domain.UserAddress;
import cn.itsource.order.mapper.OrderAddressMapper;
import cn.itsource.order.mapper.OrderAdoptMapper;
import cn.itsource.order.mapper.UserAddressMapper;
import cn.itsource.org.domain.Department;
import cn.itsource.org.domain.Product;
import cn.itsource.org.domain.ProductDetail;
import cn.itsource.org.domain.User;
import cn.itsource.org.mapper.ProductDetailMapper;
import cn.itsource.org.mapper.ProductMapper;
import cn.itsource.org.mapper.UserMapper;
import cn.itsource.org.pojo.Do.LoginDo;
import cn.itsource.org.pojo.loginInfo.LoginInfo;
import cn.itsource.org.pojo.vo.LoginVo;
import cn.itsource.org.query.ProductQuery;
import cn.itsource.org.query.UserQuery;
import cn.itsource.org.service.IProductService;
import cn.itsource.org.service.IUserService;
import cn.itsource.pay.service.AlipayService;
import cn.itsource.pet.domain.Pet;
import cn.itsource.pet.mapper.PetMapper;
import cn.itsource.pet.query.PetQuery;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
@Slf4j
public class ProductServiceImpl extends BaseServiceImpl<Product, ProductQuery> implements IProductService {
    @Autowired
    private AlipayService alipayService;
    @Resource
    private ProductMapper productMapper;
    @Resource
    private ProductDetailMapper productDetailMapper;
    @Autowired
    private UserAddressMapper userAddressMapper;
    @Autowired
    private OrderAddressMapper orderAddressMapper;
    @Autowired
    private OrderAdoptMapper orderAdoptMapper;
    @Override
    public PageList<Product> findPage(ProductQuery productQuery) {
        PageHelper.startPage(productQuery.getPage(),productQuery.getSize());
        List<Product> page = productMapper.findPage(productQuery);
        PageInfo<Product> pageInfo = new PageInfo<>(page);
        return new PageList<>(pageInfo.getTotal(),pageInfo.getList());
    }

    @Override
    public void petShelve(Long id) {
        Product product = productMapper.selectByPrimaryKey(id);
        if(product.getState()==1){
            throw new BusinessException("此商品已经上线");
        }
        product.setState(1L);
        product.setOnsaletime(new Date());
        productMapper.updateByPrimaryKeySelective(product);
    }

    @Override
    public void downShelve(Long id) {
        Product product = productMapper.selectByPrimaryKey(id);
        product.setState(0L);
        productMapper.updateByPrimaryKeySelective(product);
    }

    @Override
    public PageList<Product> findFPage(ProductQuery productQuery) {
        PageHelper.startPage(productQuery.getPage(),productQuery.getSize());
        List<Product> page = productMapper.findFPage(productQuery);
        PageInfo<Product> pageInfo = new PageInfo<>(page);
        return new PageList<>(pageInfo.getTotal(),pageInfo.getList());

    }

    @Override
    public Product petDetail(Long id) {
        Product product = productMapper.selectByPrimaryKey(id);
        return product;
    }

    @Override
    public Product getPrice(ParamDo paramDo) {
        Long petId = paramDo.getPetId();
        return productMapper.selectByPrimaryKey(petId);
    }

    @Override
    public JsonResult submitOrder(ParamDo paramDo) {
        //获取地址userid查地址
        Long userAddressId = paramDo.getAddressId();
        //查地址
        UserAddress userAddress = userAddressMapper.selectByPrimaryKey(userAddressId);
        //插入订单地址表
        OrderAddress orderAddress = BeanUtil.copyProperties(userAddress, OrderAddress.class);
        orderAddress.setId(null);
        orderAddressMapper.insertSelective(orderAddress);
        //查宠物表
        Product product = productMapper.selectByPrimaryKey(paramDo.getPetId());
        //准备更新订单表
        OrderAdopt orderAdopt = new OrderAdopt();
        orderAdopt.setDigest("宠物订单:"+ JSON.toJSONString(paramDo));
        orderAdopt.setPrice(BigDecimal.valueOf(product.getSaleprice()));
        orderAdopt.setPetId(paramDo.getPetId());
        String orderNo = IdUtil.getSnowflake().nextIdStr();
        orderAdopt.setOrderSn(orderNo);
        orderAdopt.setUserId(LoginInfoUtil.getUserId());
        orderAdopt.setAddressId(orderAddress.getId());
        orderAdoptMapper.insertSelective(orderAdopt);
        return JsonResult.createSuccess("成功",alipayService.createPayUrl(orderNo,product.getSaleprice(), "宋睿睿"));
    }

    @Override
    public void saveOrUpdate(Product product) {
        if (product.getId() == null){
            product.setState(0L);
            productMapper.insertSelective(product);
            Long id = product.getId();
            ProductDetail productDetail = new ProductDetail();
            productDetail.setProductId(id);
            productDetail.setIntro(product.getIntro());
            productDetail.setOrderNotice(product.getAdoptNotice());
            productDetailMapper.insertSelective(productDetail);
        }else {
            productMapper.updateByPrimaryKeySelective(product);
        }
    }
}
